package ac.zlf.springmybatis.core;

import org.junit.Test;

import java.io.*;

/**
 * 删除掉反编译后每行的注释
 * <p>
 * Created by Administrator on 2017/4/9 0009.
 */
public class RemoveAnnotation {

    @Test
    public void run() {
        File file = new File("C:\\Users\\Administrator\\Desktop\\aa");
        if(file.isDirectory()){
            openDir(file);
        }else {
            removeAnntation(file);
        }
    }

    public void openDir(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                if (f.isDirectory()) {
                    openDir(f);
                } else {
                    //读取文件
                    removeAnntation(f);
                }
            }
        }
    }

    public void removeAnntation(File file) {
        if (!file.isDirectory()) {
            BufferedReader bufferedReader = null;
            FileOutputStream fileOutputStream = null;
            StringBuffer sb = new StringBuffer();
            try {
                bufferedReader = new BufferedReader(new FileReader(file));
                String readLine =null;
                while ((readLine =bufferedReader.readLine())!= null){
                    readLine = readLine.replaceAll("/\\*(.|[\\r\\n])*?\\*/","");
                    sb.append(readLine);
                    sb.append("\n");
                }
                fileOutputStream = new FileOutputStream(file);
                fileOutputStream.write(sb.toString().getBytes());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(bufferedReader != null){
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(fileOutputStream != null){
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            openDir(file);
        }
    }
    @Test
    public void replaceRegex(){
        String str="/* sssso0.  */dasss..so..";
        System.out.println(str.replaceAll("/\\*(.|[\\r\\n])*?\\*/",""));
        System.out.println(str);
        System.out.println(str.replaceAll("d[.]?",""));
    }
}
